package com.kingdee.eas.fdc.customerservice.app;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.codingrule.CodingRuleException;
import com.kingdee.eas.base.codingrule.CodingRuleManagerFactory;
import com.kingdee.eas.base.codingrule.ICodingRuleManager;
import com.kingdee.eas.basedata.org.OrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fdc.customerservice.FDCStdQuestionInfo;
import com.kingdee.eas.framework.DataBaseInfo;
import com.kingdee.eas.util.app.ContextUtil;
import org.apache.log4j.Logger;

public class FDCStdQuestionControllerBean extends AbstractFDCStdQuestionControllerBean
{
  private static Logger logger = Logger.getLogger("com.kingdee.eas.fdc.customerservice.app.FDCStdQuestionControllerBean");

  protected IObjectPK _save(Context ctx, IObjectValue model)
    throws BOSException, EASBizException
  {
    FDCStdQuestionInfo info = (FDCStdQuestionInfo)model;
    if ((info.getId() == null) || (_exists(ctx, new ObjectUuidPK(info.getId()))) || (info.getNumber() == null) || (info.getNumber().length() == 0)) {
      handleIntermitNumber(ctx, info);
    }
    return super._save(ctx, model);
  }

  protected IObjectPK _submit(Context ctx, IObjectValue model)
    throws BOSException, EASBizException
  {
    FDCStdQuestionInfo info = (FDCStdQuestionInfo)model;
    if ((info.getId() == null) || (_exists(ctx, new ObjectUuidPK(info.getId()))) || (info.getNumber() == null) || (info.getNumber().length() == 0)) {
      handleIntermitNumber(ctx, info);
    }
    _checkNameDup(ctx, (DataBaseInfo)model);
    return super._submit(ctx, model);
  }

  protected IObjectPK _addnew(Context ctx, IObjectValue model)
    throws BOSException, EASBizException
  {
    FDCStdQuestionInfo info = (FDCStdQuestionInfo)model;
    if ((info.getId() == null) || (_exists(ctx, new ObjectUuidPK(info.getId()))) || (info.getNumber() == null) || (info.getNumber().length() == 0)) {
      handleIntermitNumber(ctx, info);
    }
    return super._addnew(ctx, model);
  }

  private void handleIntermitNumber(Context ctx, FDCStdQuestionInfo bill)
  {
    if (bill.getNumber() != null) {
      return;
    }
    try
    {
      ICodingRuleManager codingRuleManager = CodingRuleManagerFactory.getLocalInstance(ctx);
      String orgUnitId = ContextUtil.getCurrentOrgUnit(ctx).getId().toString();
      if (!codingRuleManager.isExist(bill, orgUnitId)) {
        return;
      }
      if ((codingRuleManager.isUseIntermitNumber(bill, orgUnitId)) || (!codingRuleManager.isAddView(bill, orgUnitId)))
      {
        String number = codingRuleManager.getNumber(bill, orgUnitId);
        bill.setNumber(number);
      }

    }
    catch (BOSException e)
    {
      logger.info(e.getMessage());
    }
    catch (EASBizException e)
    {
      logger.info(e.getMessage());
    }
  }

  protected void _delete(Context ctx, IObjectPK pk)
    throws BOSException, EASBizException
  {
    recycleNumber(ctx, pk);
    super._delete(ctx, pk);
  }

  protected void _delete(Context ctx, IObjectPK[] arrayPK)
    throws BOSException, EASBizException
  {
    for (int i = 0; i < arrayPK.length; i++) {
      recycleNumber(ctx, arrayPK[i]);
    }

    super._delete(ctx, arrayPK);
  }

  private void recycleNumber(Context ctx, IObjectPK pk) throws BOSException, EASBizException, CodingRuleException {
    FDCStdQuestionInfo info = (FDCStdQuestionInfo)getValue(ctx, pk);
    OrgUnitInfo currentCostUnit = ContextUtil.getCurrentCostUnit(ctx);
    if (currentCostUnit == null) {
      currentCostUnit = ContextUtil.getCurrentSaleUnit(ctx);
    }
    String curOrgId = currentCostUnit.getId().toString();
    if ((info.getNumber() != null) && (info.getNumber().length() > 0)) {
      ICodingRuleManager iCodingRuleManager = CodingRuleManagerFactory.getLocalInstance(ctx);
      if ((iCodingRuleManager.isExist(info, curOrgId)) && (iCodingRuleManager.isUseIntermitNumber(info, curOrgId)))
        iCodingRuleManager.recycleNumber(info, curOrgId, info.getNumber());
    }
  }
}